// FrogRenameAutoAlphaAppendDialog.cpp : Implementation of CFrogRenameAutoAlphaAppendDialog

#include "stdafx.h"
#include "FrogRenameAutoAlphaAppendDialog.h"


// CFrogRenameAutoAlphaAppendDialog
CFrogRenameAutoAlphaAppendDialog::CFrogRenameAutoAlphaAppendDialog(HICON hIcon):
CFrogOrderFilesDialog(hIcon),
m_Postfix(L""),
m_Prefix(L"")
{
}

CFrogRenameAutoAlphaAppendDialog::~CFrogRenameAutoAlphaAppendDialog()
{
}

LRESULT CFrogRenameAutoAlphaAppendDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogOrderFilesDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);
	DoDataExchange(FALSE);
	return 0;
}

LRESULT CFrogRenameAutoAlphaAppendDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);
	UpdateSortedFilesList();

	if (m_Prefix!= L"" && !FilerFrogUtils::IsFileNameValid(wstring(m_Prefix)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	if (m_Postfix!= L"" && !FilerFrogUtils::IsFileNameValid(wstring(m_Postfix)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	RunAsyncCommandWithProgressBar(false);
	EndDialog(wID);
	return 0;
}

void CFrogRenameAutoAlphaAppendDialog::ExecuteCommand()
{
	m_FilesRefactor.RenameFilesAlphabetizeAppend(m_LastResult, m_OutFilesList, (wstring)m_Prefix, (wstring)m_Postfix);
}

LRESULT CFrogRenameAutoAlphaAppendDialog::OnEnChangeEditPostfix(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	SetPostPrePreviewAutoNumAlpha(L"A");
	return 0;
}

LRESULT CFrogRenameAutoAlphaAppendDialog::OnEnChangeEditPrefix(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	SetPostPrePreviewAutoNumAlpha(L"A");
	return 0;
}

void CFrogRenameAutoAlphaAppendDialog::SetPostPrePreviewAutoNumAlpha(wstring runningChar, wstring originalName)
{	
	wstring fileName = FilerFrogUtils::GetFileNameWithoutExtension(originalName);
	wstring fileExt = FilerFrogUtils::GetFileNameExtension(originalName);

	wstring newName = wstring(m_Prefix) + runningChar + wstring(m_Postfix) + L"." + fileExt;

	wchar_t buffer[1024];
	swprintf_s(buffer, 1024, L"Before: %s\r\nAfter: %s", originalName.c_str(), newName.c_str());

	SetDlgItemText(IDC_STATIC_RENAME_PREVIEW, buffer);
}

